package com.test.work;

/**
 * @author Cc
 * 2024-03-21
 */
public class BankAccountThread extends Thread {
    /**
     * 余额
     */
    private static double balance = 5000;

    /**
     * 取款金额
     */
    private double money;

    public BankAccountThread(double money) {
        this.money = money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public void run() {
        synchronized (BankAccountThread.class) {
            if (balance < this.money) {
                System.out.println(Thread.currentThread().getName() + "尝试取钱");
                System.out.println("余额不足");
                return;
            }
            balance = balance - this.money;
            System.out.println(Thread.currentThread().getName() + "取款" + this.money + "元");
            System.out.println("余额:" + balance + "元");
        }
    }
}
